package jp.eisbahn.eclipse.plugins.lingrclipse.view;

import jp.eisbahn.eclipse.plugins.lingrclipse.LingrOccupant;

import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;

public class OccupantListLabelProvider extends LabelProvider implements ITableLabelProvider, ITableColorProvider {

	public Image getColumnImage(Object element, int columnIndex) {
		LingrOccupant occupant = (LingrOccupant)element;
		switch (columnIndex) {
			case 0:
				String iconUrl = occupant.getIconUrl();
				if (iconUrl != null)  {
					return Activator.getDefault().createImageDescriptor(getIconSmallUrl(iconUrl)).createImage();
				} else {
					return null;
				}
			default:
				break;
		}
		return null;
	}

	public String getColumnText(Object element, int columnIndex) {
		LingrOccupant occupant = (LingrOccupant)element;
		switch (columnIndex) {
			case 0:
				return occupant.getNickName();
			default:
				break;
		}
		return null;
	}
	
	private String getIconSmallUrl(String iconUrl) {
		String newUrl = iconUrl.replaceAll("[2348][0268]x[2348][0268]\\.gif", "26x26.gif");
		return newUrl;
	}

	public Color getBackground(Object element, int columnIndex) {
		return null;
	}

	public Color getForeground(Object element, int columnIndex) {
		LingrOccupant occupant = (LingrOccupant)element;
		switch(columnIndex) {
			case 0:
				String clientType = occupant.getClientType();
				if (clientType.equals("human")) {
					return Activator.getDefault().getColorProvider().getColor(new RGB(121, 59, 0));
				} else {
					return Activator.getDefault().getColorProvider().getColor(new RGB(0xcc, 0xcc, 0xcc));
				}
			default:
				break;
		}
		return null;
	}

}
